/**
 * opportunitylog服务实现
 *
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/1/15
 */
package com.sxf.crm.service.impl;


import com.sxf.crm.dto.OpportunityLogDTO;
import com.sxf.crm.entity.OpportunityLog;
import com.sxf.crm.entity.Opportunity;
import com.sxf.crm.entity.User;
import com.sxf.crm.enums.OpportunityStatus;
import com.sxf.crm.enums.OpportunityLogOperationType;
import com.sxf.crm.repository.OpportunityLogRepository;
import com.sxf.crm.service.OpportunityLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Transactional
public class OpportunityLogServiceImpl implements OpportunityLogService {

    private final OpportunityLogRepository opportunityLogRepository;

    @Override
    public OpportunityLog create(OpportunityLog log) {
        log.setOperationTime(Instant.now().toEpochMilli());
        return opportunityLogRepository.save(log);
    }

    @Override
    public OpportunityLog logStatusChange(Opportunity opportunity, User operator, OpportunityStatus before, OpportunityStatus after) {
        OpportunityLog log = OpportunityLog.builder()
                .opportunity(opportunity)
                .operator(operator)
                .operationType(OpportunityLogOperationType.STATUS_CHANGE)
                .operationTime(System.currentTimeMillis())
                .operationDesc("修改状态")
                .statusBefore(before)
                .statusAfter(after)
                .build();
        return create(log);
    }

    @Override
    public OpportunityLog logAssign(Opportunity opportunity, User operator, User newOwner, String remark) {
        OpportunityLog log = OpportunityLog.builder()
                .opportunity(opportunity)
                .operator(operator)
                .operationType(OpportunityLogOperationType.ASSIGN)
                .operationTime(System.currentTimeMillis())
                .operationDesc("分配给" + newOwner.getUsername())
                .details(remark)
                .build();
        return create(log);
    }

    @Override
    public OpportunityLog logCreate(Opportunity opportunity, User creator, String opportunityName) {
        OpportunityLog log = OpportunityLog.builder()
                .opportunity(opportunity)
                .operator(creator)
                .operationType(OpportunityLogOperationType.CREATE)
                .operationTime(System.currentTimeMillis())
                .operationDesc("创建商机" + opportunityName)
                .statusAfter(opportunity.getStatus())
                .build();
        return create(log);
    }

    @Override
    public OpportunityLog logFollowUp(Opportunity opportunity, User operator, String followUpContent) {
        OpportunityLog log = OpportunityLog.builder()
                .opportunity(opportunity)
                .operator(operator)
                .operationType(OpportunityLogOperationType.FOLLOW_UP)
                .operationTime(System.currentTimeMillis())
                .operationDesc("跟进记录")
                .details(followUpContent)
                .build();
        return create(log);
    }

    @Override
    public List<OpportunityLogDTO> findByOpportunityId(Long opportunityId) {
        return opportunityLogRepository.findByOpportunityIdOrderByOperationTimeDesc(opportunityId)
                .stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    private OpportunityLogDTO convertToDTO(OpportunityLog log) {
        OpportunityLogDTO dto = new OpportunityLogDTO();
        BeanUtils.copyProperties(log, dto);
        dto.setOpportunityId(log.getOpportunity().getId());
        dto.setOperatorId(log.getOperator().getId());
        dto.setOperatorName(log.getOperator().getUsername());
        dto.setOperationTypeLabel(log.getOperationType().getLabel());
        return dto;
    }

    @Override
    public OpportunityLog logAccept(Opportunity opportunity, User operator, User owner) {
        OpportunityLog log = OpportunityLog.builder()
                .opportunity(opportunity)
                .operator(operator)
                .operationType(OpportunityLogOperationType.ACCEPT)
                .operationTime(System.currentTimeMillis())
                .operationDesc("接受商机")
                .build();
                return log;
    }

    @Override
    public OpportunityLog logReject(Opportunity opportunity, User operator, User owner, String rejectReason) {
        OpportunityLog log = OpportunityLog.builder()
                .opportunity(opportunity)
                .operator(operator)
                .operationType(OpportunityLogOperationType.REJECT)
                .operationTime(System.currentTimeMillis())
                .operationDesc("拒绝商机")
                .details(rejectReason)
                .build();
        return log;
    }

    @Override
    public OpportunityLog logRecycle(Opportunity opportunity, User operator, String recycleReason) {
        OpportunityLog log = OpportunityLog.builder()
                .opportunity(opportunity)
                .operator(operator)
                .operationType(OpportunityLogOperationType.RECYCLE)
                .operationTime(System.currentTimeMillis())
                .operationDesc("回收商机")
                .details(recycleReason)
                .build();
        return log;
    }

    @Override
    public OpportunityLog logLost(Opportunity opportunity, User operator, String lostReason) {
        OpportunityLog log = OpportunityLog.builder()
                .opportunity(opportunity)
                .operator(operator)
                .operationType(OpportunityLogOperationType.LOST)
                .operationTime(System.currentTimeMillis())  
                .operationDesc("丢失商机")
                .details(lostReason)
                .build();
        return log;
    }

    @Override
    public OpportunityLog logWon(Opportunity opportunity, User operator, String remark) {
        OpportunityLog log = OpportunityLog.builder()
                .opportunity(opportunity)
                .operator(operator)
                .operationType(OpportunityLogOperationType.WON)
                .operationTime(System.currentTimeMillis())
                .operationDesc("成交商机")
                .details(remark)
                .build();
        return log;
    }

    @Override
    public OpportunityLog logNegotiating(Opportunity opportunity, User operator, String remark) {
        OpportunityLog log = OpportunityLog.builder()
                .opportunity(opportunity)
                .operator(operator)
                .operationType(OpportunityLogOperationType.NEGOTIATING)
                .operationTime(System.currentTimeMillis())
                .operationDesc("谈判中")
                .details(remark)
                .build();
        return log; 
    }

    @Override
    public OpportunityLog logPendingDecision(Opportunity opportunity, User operator) {
        OpportunityLog log = OpportunityLog.builder()
                .opportunity(opportunity)
                .operator(operator)
                .operationType(OpportunityLogOperationType.PENDING_DECISION)
                .operationTime(System.currentTimeMillis())  
                .operationDesc("待决策")
                .build();
        return log;
    }

    @Override
    public OpportunityLog logClaim(Opportunity opportunity, User operator, User newOwner, String remark) {
        OpportunityLog log = OpportunityLog.builder()
                .opportunity(opportunity)
                .operator(operator)
                .operationType(OpportunityLogOperationType.CLAIM)
                .operationTime(System.currentTimeMillis())
                .operationDesc("认领商机")
                .details(remark)
                .build();
        return log;
    }
}
